
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Media.Animation;
using Presentation.Pages;
using Presentation.Pages.Collection;
using System.Linq;
using System.Windows.Input;

namespace Microsoft.Samples.Graphics.Geometries
{

    public partial class SampleViewer : Page
    {
       private Page[] examples;
       private int sampleIndex;
       
       public SampleViewer()
       {      
            InitializeComponent();
            
            Keyboard.AddKeyDownHandler(this, SampleViewer_KeyDown);
            //examples[0] = new GeometryUsageExample();
            //examples[1] = new ShapeGeometriesExample(); 
            //examples[2] = new PathGeometryExample();
            //examples[3] = new CombiningGeometriesExample();
            //examples[4] = new GeometryAttributeSyntaxExample();     
       
       }

       void page_MouseDown(object sender, MouseButtonEventArgs e)
       {
           if (e.LeftButton == MouseButtonState.Pressed
              && e.RightButton == MouseButtonState.Released)
           {
               if (stackPanel.Children.Count > sampleIndex + 1 &&
                              stackPanel.Children[sampleIndex + 1] is RadioButton)
                   (stackPanel.Children[sampleIndex + 1] as RadioButton).IsChecked = true;
           }
           else if (e.LeftButton == MouseButtonState.Released
               && e.RightButton == MouseButtonState.Pressed)
           {
               if (sampleIndex > 0 && stackPanel.Children.Count > 0 &&
                             stackPanel.Children[sampleIndex - 1] is RadioButton)
                   (stackPanel.Children[sampleIndex - 1] as RadioButton).IsChecked = true;
           }
       }

       void SampleViewer_KeyDown(object sender, KeyEventArgs e)
       {
           if (e.Key == Key.Space)
           {
               
           }
       }

       //protected override void OnMouseDown(MouseButtonEventArgs e)
       //{
       //    if (e.LeftButton == MouseButtonState.Pressed
       //        && e.RightButton == MouseButtonState.Released)
       //    {
       //        if (stackPanel.Children.Count > sampleIndex &&
       //                       stackPanel.Children[sampleIndex + 1] is RadioButton)
       //            (stackPanel.Children[sampleIndex + 1] as RadioButton).IsChecked = true;
       //    }
       //    else if (e.LeftButton == MouseButtonState.Released
       //        && e.RightButton == MouseButtonState.Pressed)
       //    {
       //        if (sampleIndex >= 0 && stackPanel.Children.Count > 0 &&
       //                      stackPanel.Children[sampleIndex + 1] is RadioButton)
       //            (stackPanel.Children[sampleIndex + 1] as RadioButton).IsChecked = true;
       //    }
       //    base.OnMouseDown(e);
       //}

       public static readonly DependencyProperty PagesProperty = DependencyProperty.Register("Pages", typeof(ViewPageCollection), typeof(SampleViewer));

       public ViewPageCollection Pages
       {
           get
           {
               return (ViewPageCollection)GetValue(PagesProperty);
           }
           set
           {
               SetValue(PagesProperty, value);

           }
       }
       
       private void pageLoaded(object sender, RoutedEventArgs args)
       {
           Pages = (ViewPageCollection)this.FindResource("pages");
           examples = new Page[5];
           stackPanel.Children.Clear();
           double maxRadioButtonWidth = 0;
           foreach (Page page in Pages)
           {
               page.MouseDown -= page_MouseDown;
               page.MouseDown += page_MouseDown;
               var rb = new RadioButton { Content = page.Title };
               if (maxRadioButtonWidth < rb.ActualWidth)
               {
                   maxRadioButtonWidth = rb.ActualWidth;
               }
               rb.Checked += sampleSelected;
               stackPanel.Children.Add(rb);
           }
           if ((Application.Current as Presentation.App).WtfPresent)
           {
               var firstPage = new PresentationPage { Width = myScrollViewer.ActualWidth, Height = myScrollViewer.ActualHeight };
               firstPage.MouseDown -= page_MouseDown;
               firstPage.MouseDown += page_MouseDown;
               mainFrame.Navigate(firstPage);
               return;
           }
           sampleIndex = 0;
           if (stackPanel.Children.Count > sampleIndex &&
                                     stackPanel.Children[sampleIndex] is RadioButton)
               (stackPanel.Children[sampleIndex] as RadioButton).IsChecked = true;
       }

       private void zoomOutStoryboardCompleted(object sender, EventArgs args)
       {
           var page = Pages[sampleIndex] as Page;
           if (page != null)
           {
               page.Width = myScrollViewer.ActualWidth;
               page.Height = myScrollViewer.ActualHeight;
               mainFrame.Navigate(Pages[sampleIndex]);
           }
       }
       
       private void frameContentRendered(object sender, EventArgs args)
       {
            
            Storyboard s = (Storyboard)this.Resources["ZoomInStoryboard"];
            s.Begin(this);
       }
       
       private void zoomInStoryboardCompleted(object sender, EventArgs e)
       {
            scrollViewerBorder.Visibility = Visibility.Visible;
       
       }
       
       
       private void sampleSelected(object sender, RoutedEventArgs args)
       {

         
         Point3DCollection points = new Point3DCollection();
         
         double ratio = myScrollViewer.ActualWidth / myScrollViewer.ActualHeight;
         
         points.Add(new Point3D(5, -5 * ratio, 0));
         points.Add(new Point3D(5, 5 * ratio, 0));
         points.Add(new Point3D(-5, 5 * ratio, 0));
         
         points.Add(new Point3D(-5, 5 * ratio, 0));
         points.Add(new Point3D(-5, -5 * ratio, 0));
         points.Add(new Point3D(5, -5 * ratio, 0));

         points.Add(new Point3D(-5, 5 * ratio, 0));
         points.Add(new Point3D(-5, -5 * ratio, 0));
         points.Add(new Point3D(5, -5 * ratio, 0));           
         
         points.Add(new Point3D(5, -5 * ratio, 0));  
         points.Add(new Point3D(5, 5 * ratio, 0));
         points.Add(new Point3D(-5, 5 * ratio, 0)); 
       
         myGeometry.Positions = points;
         myViewport3D.Width = 100;
         myViewport3D.Height = 100 * ratio;
         
         scrollViewerBorder.Visibility = Visibility.Hidden; 
         
         RadioButton button = sender as RadioButton;

         if (button != null)
         {
             sampleIndex = stackPanel.Children.IndexOf(button);
         
           //if (button.Content.ToString() == "Geometry Usage")
           //  sampleIndex = 0;
           //else if (button.Content.ToString() == "Shape Geometries")
           // sampleIndex = 1;

           //else if (button.Content.ToString() == "PathGeometry")
           // sampleIndex = 2;

           //else if (button.Content.ToString() == "Combining Geometries Example")
           //  sampleIndex = 3;
           //else if (button.Content.ToString() == "Geometry Attribute Syntax Example")
           // sampleIndex = 4;            
            
         }           
       }  
    }
}